package com.hz.pro.kitchen.config;

import com.google.common.base.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Slf4j
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket createRestApi() {
        Predicate<RequestHandler> predicate = (input) -> {
            Class<?> declaringClass = input.declaringClass();
            //排除
            if (excludeClass(declaringClass)) {
                return false;
            }
            //被注解的类
            if (declaringClass.isAnnotationPresent(RestController.class)) {
                return true;
            }
            //被注解的方法
            if (input.isAnnotatedWith(ResponseBody.class)) {
                return true;
            }
            return false;
        };
        //添加head参数start
        List<Parameter> pars = new ArrayList<>();
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .select()
                .apis(predicate)
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }

    private boolean excludeClass(Class<?> declaringClass) {
        return false;
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API文档 项目规范地址：http://www.baidu.com")
                .description("特别说明 无")
                .version("1.0.0")
                .termsOfServiceUrl("http://www.baidu.com")
                .build();
    }
}